package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.*;
import com.it.tydic.estate.module.mapper.inner.*;
import com.it.tydic.estate.module.model.inner.*;
import com.it.tydic.estate.module.service.MoveOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: liuhui
 * @createDate: 2020/3/4
 * @version: 1.0
 */
@Service
@Transactional
public class MoveOutServiceImpl implements MoveOutService {
    @Autowired
    private TbMoveOutMapper tbMoveOutMapper;    /*迁出*/
    @Autowired
    private TbMoveInMapper moveInMapper;    /*迁入*/
    @Autowired
    private FangjianguanliMapper fangjianguanliMapper;  /*房间*/
    @Autowired
    private TbHistoryMapper tbHistoryMapper;    /*历史记录*/
    @Autowired
    private TbCustomerProfileMapper tbCustomerProfileMapper;    /*档案*/

    /**
     * 迁出数据列表
     * @param map
     * @return
     */
    @Override
    public Result selectListByLike(Map<String, Object> map) {
        /*分页*/
//        if(map.containsKey("pageNum") && map.containsKey("pageSize")){//判断是否有分页条件有进行分页
//            PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
//        }
        CommonUtils.pageProcess(map);//验证处理分页
        Page<TbMoveIn> tbMoveOuts = tbMoveOutMapper.selectListByLike(map);
        List<TbMoveIn> result = tbMoveOuts.getResult();
        if(result.size()> 0 && result != null){
            /*码表赋值*/
            for (TbMoveIn tbMoveIn : result) {
                tbMoveIn.setFjzt(DictionaryCache.getDicVal(tbMoveIn.getFjzt()));
                tbMoveIn.setFjlx(DictionaryCache.getDicVal(tbMoveIn.getFjlx()));
                tbMoveIn.setSffwdx(DictionaryCache.getDicVal(tbMoveIn.getSffwdx()));
            }
        }
        /*返回集合*/
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("result",result);
        resultMap.put("totle",tbMoveOuts.getTotal());
        return Result.success(resultMap);
    }

    /**获取详情
     *
     * @param uid
     * @return
     */
    @Override
    public Result getById(String uid) {
        /*验证参数*/
        if(StringUtils.isNotEmpty(uid)){
            Map<String,Object> map = new HashMap<>();
            map.put("uid",uid);
            List<TbMoveIn> result = tbMoveOutMapper.selectListByLike(map).getResult();
            return Result.success(result);
        }
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }

    /**
     * 迁出数据添加
     * @param tbMoveOut
     * @return
     */
    @Override
    public Result addMoveOut(TbMoveOut tbMoveOut) {
        InstaIdGenerator idGenerator = new InstaIdGenerator();
        /*生成id*/
        long l = idGenerator.nextId(TimeUtil.timeGen());
        tbMoveOut.setUid(String.valueOf(l));
        tbMoveOut.setUpdateTime(new Date());
        tbMoveOut.setOperator(CurrentUtil.getCurrentUserId());
        /*添加迁出信息*/
        int num = tbMoveOutMapper.insertSelective(tbMoveOut);
        /*修改房间状态*/
        Fangjianguanli fangjianguanli = new Fangjianguanli();
        fangjianguanli.setId(tbMoveOut.getRoomId());
        fangjianguanli.setSffwdx(tbMoveOut.getMoveOutObj());    /*迁出后房间收费对象*/
        fangjianguanli.setFjzt(tbMoveOut.getMoveOutRoomType());
        fangjianguanli.setUuserId(CurrentUtil.getCurrentUserId());
        fangjianguanli.setXgsj(new Date());
        fangjianguanliMapper.updateByPrimaryKeySelective(fangjianguanli);
        /*返回集合*/
        Map<String,Object> map = new HashMap<>();
        map.put("roomId",tbMoveOut.getRoomId());
        map.put("zfbj",ZfbjFinalUtil.zfbj_zy);
        Page<TbCustomerProfile> tbCustomerProfiles = tbCustomerProfileMapper.selectLink(map);
        if(tbCustomerProfiles.size()==0){
           return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        TbCustomerProfile tbCustomerProfile = new TbCustomerProfile();
        tbCustomerProfile.setUid(String.valueOf(tbCustomerProfiles.getResult().get(0).getUid()));
        tbCustomerProfile.setZfbj(ZfbjFinalUtil.zfbj_zf);
        tbCustomerProfile.setUuserId(CurrentUtil.getCurrentUserId()); /*修改人*/
        tbCustomerProfile.setXgsj(new Date());
        /*修改档案信息*/
        tbCustomerProfileMapper.updateByPrimaryKeySelective(tbCustomerProfile);
        // 添加历史数据
        TbHistory tbHistory = new TbHistory();
        tbHistory.setUid(String.valueOf(idGenerator.nextId(TimeUtil.timeGen())));
        tbHistory.setFjid(tbMoveOut.getRoomId());
        tbHistory.setMoveid(tbMoveOut.getMoveinId());
        tbHistory.setNewdate(new Date());   /*添加时间*/
        tbHistory.setUpdateTime(new Date());  /*修改时间*/
        tbHistory.setOperator(CurrentUtil.getCurrentUserId());  /*修改人*/
        tbHistory.setProfileid(String.valueOf(tbCustomerProfiles.getResult().get(0).getUid()));
        tbHistory.setType(ZfbjFinalUtil.zfbj_zf);   /*迁出*/
        tbHistory.setZfbj(ZfbjFinalUtil.zfbj_zy);
        tbHistory.setMoveoutid((String.valueOf(l)));
        /*获取用户名*/
        tbHistory.setBz("迁出历史记录");
        tbHistoryMapper.insertSelective(tbHistory);
        /*修改迁入信息*/
        TbMoveIn tbMoveIn = new TbMoveIn();
        tbMoveIn.setRoomId(tbMoveOut.getRoomId());
        tbMoveIn.setZfbj(ZfbjFinalUtil.zfbj_zf);        /*将迁入信息修改作废*/
        tbMoveIn.setOperator(CurrentUtil.getCurrentUserId());
        tbMoveIn.setUpdateTime(new Date());
        tbMoveIn.setUid(tbMoveOut.getMoveinId());
        moveInMapper.updateMoveIn(tbMoveIn);
        return num > 0 ? Result.success() : Result.error(CodeMsg.INSERT_ERROR);
    }
}
